package com.eascs.admin.o2o.receivedRecords.controller;

import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.app.dao.PageResult;
//import com.eascs.app.log.Logger;
//import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
//import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;
import java.text.DecimalFormat;
import java.text.Format;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.eascs.admin.o2o.receivedRecords.entity.ReceivedRecordsEntity;
import com.eascs.admin.o2o.receivedRecords.entity.ReceivedRecordsVo;
import com.eascs.admin.o2o.receivedRecords.service.ReceivedRecordsService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
//--------------------
@Controller
@RequestMapping("/receivedRecords")
public class ReceivedRecordsController extends BaseMasterViewController{//try
//	private final Logger logger = LoggerFactory.getLogger(ReceivedRecordsController.class);
	@Reference
	SysOrgService sysOrgService;
	@Autowired
	private ReceivedRecordsService service;//待写的service
	
	/**
	 * @Description:
	 * 				<p>
	 *               * 回款记录数据查询
	 *               </p>
	 * @author LQ
	 * @date 2018年1月8日
	 * @version V1.0
	 * @param vo
	 * @return
	 */
	@RequestMapping("/index")
	public String index(ReceivedRecordsVo vo){//待写的vo
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "HKJLData");
		vo.setPageSize(10);
		List<ActOrgEntity> orgProvince = sysOrgService.getOrgProvince();
		ViewContext.put("ActOrgEntitys", orgProvince);//省区
		String o2ourl = "/receivedRecords/index";
		String billCode = vo.getBillCode() == null ? "" : vo.getBillCode();//贷款单号
		String loanBank = vo.getLoanBank() == null ? "" : vo.getLoanBank();//贷款银行
		String operatorName   = vo.getOperatorName()   == null ? "" : vo.getOperatorName();//信贷经理
		String loanerName = vo.getLoanerName() == null ? "" : vo.getLoanerName();//借款人姓名
		String provinceCode = vo.getProvinceCode() == null ? "" : vo.getProvinceCode();//省区code
		o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
		o2ourl = UrlUtil.appendUrl(o2ourl, "loanBank", loanBank);
		o2ourl = UrlUtil.appendUrl(o2ourl, "operatorName", operatorName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "loanerName", loanerName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "provinceCode", provinceCode);
		o2ourl = o2ourl + "&pageIndex=";
		//====
		List<User> users = sysOrgService.checkDataPermission(UserHolder.getUser().getNo());
		String no = "";
		if (users != null) {
			for (User user : users) {
				no += user.getNo() + ",";
			}
		}
		if (!"".equals(no)) {
			if (",".equals(no.substring(no.length() - 1, no.length()))) {
				no = no.substring(0, no.length() - 1);
			}
		}
		vo.setUsersno(no);
		//====
		PageResult<Object[]> pageResult = service.getHKJLByPageService(vo);//待改========
		String titles[] = { "贷款单号",  "省区", "信贷经理" , "借款人姓名", "放款银行", "放款金额", "回款金额", "最后回款日期" };// ,
		Format[] formats = new Format[] { null, null, null, null, null, new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00"), null};// ,
		//------------------------------------------------数据库中查出来的是 province_code
//		SelectDataOption[] selectDataOptions = { null, new SelectDataOption("PROVINCE"), null, null, new SelectDataOption("BANKTYPEO2O"), null, null, null};
		//此三个按钮需做功能权限-------------待完成
		Button button1 = ButtonHelper.createOpenButton("查看明细", "/receivedRecords/detail?id={0}");
		button1.setPermissionCode("receivedPaymentsRecordsDetail");	
//		Button button2 = ButtonHelper.createAjaxButton("新增", "/add");
		Button button3 = ButtonHelper.createAjaxConfirmButton("删除", "/rReceived/del?id={0}");
		
		button3.setPermissionCode("receivedPaymentsRecordsDel");
		button3.setConfirmMessage("确定删除?");
		Button[] detailButton = new Button[] {button1,button3 };
//		Button[] detailButton = new Button[] {button1,button2,button3 };
		//=====================================
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setBuildPagingNavigation(true);
		o2oPagingInfo.setMoreButtons(detailButton);
		o2oPagingInfo.setFirstRenderColumnIndex(2);
//		o2oPagingInfo.setSelectDataOptions(selectDataOptions);//----------
		o2oPagingInfo.setFormats(formats);
		ViewContext.put("paginationInfo", o2oPagingInfo);
		ViewContext.put("vo", vo);
		return "/receivedRecords/index";
	}
	
	@RequestMapping(value = "/add_view", method = RequestMethod.GET)
	public String addRecord() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "addNewReceivedRecords");// 添加菜单标签tag
		return "/receivedRecords/update_next";// 跳转到update_next下一步页面
	}
//	@RequestMapping("/add_next")
//		public String updateRecord(String billCode) {
//		if (StringUtils.isEmpty(billCode)) {// 获取贷款单号
//			return .warn("贷款单号为空，请输入");//待添加提示对话框----------------------------
//		} 
//		ReceivedRecordsVo vo = service.findReceivedRecordsEntity(billCode);
//		ViewContext.put("vo",vo);
//		return "receivedRecords/update_info";
//	}
	@RequestMapping("/detail")
	public  String findDetail(String id) {
		ReceivedRecordsVo v = service.getBillCodeById(id);//根据id查出billCode
		ReceivedRecordsVo vo =service.findReceivedRecordsEntity(v.getBillCode());
		ReceivedRecordsEntity entity=service.getHKJLById(id);//查出逾期回款明细
		//-------增加权限验证????
		List<User> users = sysOrgService.checkDataPermission(UserHolder.getUser().getNo());
		String no = "";
		if (users != null) {
			for (User user : users) {
				no += user.getNo() + ",";
			}
		}
		if (!"".equals(no)) {
			if (",".equals(no.substring(no.length() - 1, no.length()))) {
				no = no.substring(0, no.length() - 1);
			}
		}
		vo.setUsersno(no);
		//-------
		ViewContext.put("vo",vo);//这个地方会不会冲突, entity与vo中同名key的value会不会相互覆盖
		ViewContext.put("entity",entity);
		return "receivedRecords/detail";
	}
}
